package com.bwie.textls;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jing.domain.Vo.ShopAttributeVO;
import com.jing.domain.dto.Linked;
import com.jing.domain.dto.Phase;
import com.jing.domain.po.ShopAttribute;
import org.springframework.beans.BeanUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author：wp
 * @Package：com.bwie.textls
 * @Project：jing
 * @name：TextTable
 * @Date：2024/6/1 10:27
 */

public class TextTable {
    public static void main(String[] args) {
        String s= "[\n" +
                "\t{\n" +
                "\t\t\"cid\": 1,\n" +
                "\t\t\"attrName\": \"黑色\"\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"cid\": 2,\n" +
                "\t\t\"attrName\": \"M\"\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"cid\": 1,\n" +
                "\t\t\"attrName\": \"红色\"\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"cid\": 2,\n" +
                "\t\t\"attrName\": \"X\"\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"cid\": 3,\n" +
                "\t\t\"attrName\": \"男\"\n" +
                "\t},\n" +
                "\t{\n" +
                "\t\t\"cid\": 3,\n" +
                "\t\t\"attrName\": \"女\"\n" +
                "\t}\n" +
                "]\n";

         //获取集合
        List<Linked> linkeds = JSONObject.parseArray ( s, Linked.class );

        // 使用流操作和Collectors.groupingBy将数据按cid分组
        Map<String, List<Linked>> groupedByCid = linkeds.stream()
                .collect(Collectors.groupingBy(
                        map -> String.valueOf(map.getId()), // 分组键
                        Collectors.toList() // 收集器
                ));
         //TODO  转换
        // 假设已经有了一个包含所有不同cid的Set
         Set<Integer> Cids = new HashSet<>(groupedByCid.keySet().stream()
                .map(Integer::parseInt)
                .collect(Collectors.toList()));
        // 获取所有可能的组合
        List<Map<String, String>> com = new ArrayList<>();
        List<List<String>> cidValues = Cids.stream()
                .map(cid -> groupedByCid.getOrDefault(String.valueOf(cid), Collections.emptyList())
                        .stream()
                        .map(Linked::getName)
                        .collect(Collectors.toList()))
                .collect(Collectors.toList());

        // 使用笛卡尔积生成所有组合
        cartesianProduct(cidValues).forEach(combination -> {
            Map<String, String> result = new LinkedHashMap<> ();
            for (int i = 0; i < combination.size(); i++) {
                result.put(String.valueOf(Cids.toArray(new Integer[0])[i]), combination.get(i));
            }
            com.add(result);
        });
        // 输出结果
        System.out.println(JSONObject.toJSONString(com));


    }
    // 计算笛卡尔积的辅助方法
    private static List<List<String>> cartesianProduct(List<List<String>> lists) {
        if (lists == null || lists.isEmpty()) {
            return Collections.emptyList();
        }
        List<List<String>> result = new ArrayList<>();
        if (lists.size() == 1) {
            // 如果只有一个列表，则直接返回它的元素作为单元素列表
            for (String item : lists.get(0)) {
                result.add(Collections.singletonList(item));
            }
        } else {
            // 递归地计算除了第一个列表之外的笛卡尔积
            List<List<String>> restCartesian = cartesianProduct(lists.subList(1, lists.size()));
            // 将第一个列表中的每个元素与剩余笛卡尔积的每个列表组合起来
            for (String first : lists.get(0)) {
                for (List<String> rest : restCartesian) {
                    List<String> combination = new ArrayList<>(rest.size() + 1);
                    combination.add(first);
                    combination.addAll(rest);
                    result.add(combination);
                }
            }
        }
        return result;
}
}
